package Scuttlebutt;

import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import glue.*;
import javax.swing.text.DefaultEditorKit;
import javax.swing.tree.DefaultMutableTreeNode;
import java.util.Enumeration;
import java.lang.*;
import java.util.*;

public class MenuBar extends JMenuBar 
{
/*
	public class OpenProjectThread implements Runnable{
		File projectDir = null;
		Scuttlebutt parent = null;
		public OpenProjectThread(Scuttlebutt parent, File projectDir){
			this.parent = parent;
			this.projectDir = projectDir;
		}
		
		public void run(){
			parent.setProject(Project.Open(projectDir));
			parent.setWaitCursor(true);
			parent.getButtonBar().getConnectButton().setEnabled(false);
			int size = parent.getProject().getOpendCount();
			Object [] openList = parent.getProject().getOpenedList();
			for(Object o : openList){
				parent.getEditPane().OpenWithoutCheck(o.toString());
			}
			
			
			parent.connect();
			recurseDirectoryForDownloadingNodes(parent.getRootNode());
			parent.getFileTransferTable().startDownloading();
			parent.setTitle();
			parent.updateTree();
			parent.getButtonBar().getConnectButton().setEnabled(true);
			parent.getButtonBar().enableSaveProjectButton(true);
			parent.getButtonBar().enableCloseProjectButton(true);
			parent.setWaitCursor(false);
		}
		
		private void recurseDirectoryForDownloadingNodes(DefaultMutableTreeNode o){
			for (Enumeration e = o.children() ; e.hasMoreElements() ;) {
				DefaultMutableTreeNode node = (DefaultMutableTreeNode)e.nextElement();
		        if (node instanceof FolderNode ) {
			        recurseDirectoryForDownloadingNodes(node);
		        }else if(node instanceof URLNode){
		        }else if (node instanceof SortableNode ){//file
			        SortableNode s = (SortableNode)node;
					File f = new File(s.getNodePath());
					if(f.exists()){					
						if(s.getProgress() > 0){
							Download d = new Download(parent, s);
							parent.getFileTransferTable().addDownload(d);
						}
			        }else{
				        parent.getProject().removeOpened(s.getNodePath());
				        parent.getEditPane().remove(s.getNodePath());
				        s.removeFromParent();
					}
		        }
			}
		}	
	}
*/	
	
	public class RecentProjectMenuItem extends JMenuItem implements ActionListener  
	{

		File projectDir = null;
		Scuttlebutt parent = null;
		public RecentProjectMenuItem(Scuttlebutt parent, String text, File projectDir)
		{
			super(text);
			this.parent = parent;
			this.projectDir = projectDir;
			addActionListener(this);
		}
	
		public void actionPerformed(ActionEvent a0)
		{
//			new Thread(new OpenProjectThread(parent, projectDir)).start();
			parent.loadProject(projectDir);
		}
	}

	public class FileMenuListener implements MenuListener{
		Scuttlebutt parent = null;
		public FileMenuListener(Scuttlebutt parent){
			this.parent = parent;
		}
		
		public void menuSelected(MenuEvent e){
			if(parent.getEditPane().getTabCount() == 0){
				getMenu(0).getItem(9).setEnabled(false);
			}else{
				if((parent.getEditPane().needsToBeSaved()) && (!parent.getFileTransferTable().isFileDownloading(parent.getEditPane().getSelectedTabFile()))){
					getMenu(0).getItem(9).setEnabled(true);
				}else{
					getMenu(0).getItem(9).setEnabled(false);
				}
			}
			
			
			if(parent.getProject().getProjectPath() == null){
				getMenu(0).getItem(0).setEnabled(false);
				getMenu(0).getItem(2).setEnabled(true);
				getMenu(0).getItem(3).setEnabled(true);
				getMenu(0).getItem(4).setEnabled(false);
				getMenu(0).getItem(5).setEnabled(false);
				getMenu(0).getItem(7).setEnabled(false);
				getMenu(0).getItem(8).setEnabled(false);
			}else{
				getMenu(0).getItem(0).setEnabled(true);
				getMenu(0).getItem(2).setEnabled(false);
				getMenu(0).getItem(3).setEnabled(false);
				getMenu(0).getItem(4).setEnabled(true);
				getMenu(0).getItem(5).setEnabled(true);
				getMenu(0).getItem(7).setEnabled(true);
				getMenu(0).getItem(8).setEnabled(true);
			}
			if(parent.isConnected()){
				getMenu(0).getItem(0).setText("Disconnect");
			}else{
				getMenu(0).getItem(0).setText("Connect");
			}
		}
		public void menuDeselected(MenuEvent e){
		}
		
		public void menuCanceled(MenuEvent e){
		}
	}

	public class EditMenuListener implements MenuListener{
		Scuttlebutt parent = null;
		public EditMenuListener(Scuttlebutt parent){
			this.parent = parent;
		}
		public void menuSelected(MenuEvent e){
				
			if(parent.getEditPane().getTabCount() == 0){
				getMenu(1).getItem(0).setEnabled(false);
				getMenu(1).getItem(1).setEnabled(false);
				getMenu(1).getItem(3).setEnabled(false);
				getMenu(1).getItem(4).setEnabled(false);
				getMenu(1).getItem(5).setEnabled(false);
				getMenu(1).getItem(6).setEnabled(false);
				getMenu(1).getItem(8).setEnabled(false);
				getMenu(1).getItem(9).setEnabled(false);
				getMenu(1).getItem(10).setEnabled(false);
//				return;
			}else{
			
				Tab t = (Tab)((JScrollPane)parent.getEditPane().getSelectedComponent()).getViewport().getView();
				getMenu(1).getItem(0).setEnabled(t.canUndo());
				getMenu(1).getItem(1).setEnabled(t.canRedo());
				getMenu(1).getItem(3).setEnabled(t.canCut());
				getMenu(1).getItem(4).setEnabled(t.canCopy());
				getMenu(1).getItem(5).setEnabled(t.canPaste());
				getMenu(1).getItem(6).setEnabled(t.canDelete());
				getMenu(1).getItem(8).setEnabled(t.canSelectAll());
				getMenu(1).getItem(9).setEnabled(t.canTime());
				getMenu(1).getItem(10).setEnabled(t.canDate());
			}
		}
		
		public void menuDeselected(MenuEvent e){
		}
		
		public void menuCanceled(MenuEvent e){
		}
	}
	
	
	public class ToolsMenuListener implements MenuListener{
		Scuttlebutt parent = null;
		int pos = 0;
		public ToolsMenuListener(Scuttlebutt parent, int pos){
			this.parent = parent;
			this.pos = pos;
		}
		public void menuSelected(MenuEvent e){
			getMenu(pos).getItem(0).setEnabled((parent.getProject().getProjectPath() != null)?true:false);
			getMenu(pos).getItem(2).setEnabled((parent.isConnected())?true:false);
		}
		
		public void menuDeselected(MenuEvent e){
		}
		
		public void menuCanceled(MenuEvent e){
		}
	}	
	
	
	public void enableMenus(boolean state){
		for(int x = 0; x < getMenuCount(); x++){
			getMenu(x).setEnabled(state);
		}
	}
	
	public MenuBar(Scuttlebutt parent)
	{
		createMenu(parent);
	}

	public void createMenu(Scuttlebutt parent){
		
		JMenu fileMenu = new JMenu("File");
		fileMenu.addMenuListener(new FileMenuListener(parent));
		add(fileMenu);
		fileMenu.add(new EventConnect(new String("Connect"), parent));
		fileMenu.addSeparator();
		fileMenu.add(new EventNewProject(new String("New Project..."), parent));
		fileMenu.add(new EventOpenProject(new String("Open Project..."), parent));
		fileMenu.add(new EventSaveProject(new String("Save Project"), parent));
		fileMenu.add(new EventCloseProject(new String("Close Project"), parent));
		fileMenu.addSeparator();
		fileMenu.add(new EventNewFile(new String("New File..."), parent));
		fileMenu.add(new EventAddFile(new String("Add File..."), parent));
		fileMenu.add(new EventSaveFile(new String("Save File"), parent));
		fileMenu.addSeparator();
		
		RecentProjects rp = RecentProjects.open();
		Object list[] = rp.getProjectNames();
		for(Object o : list){
			fileMenu.add( new RecentProjectMenuItem(parent, o.toString(), rp.getProjectPath(o.toString())));
		}
		
	
		fileMenu.addSeparator();
		fileMenu.add(new EventExit(new String("Exit"), parent));

		
		JMenu editMenu = new JMenu("Edit");
		editMenu.addMenuListener(new EditMenuListener(parent));
		add(editMenu);

		JMenuItem menuItem = null;	
		editMenu.add(new UndoMenuItem(parent.getEditPane()));
		editMenu.add(new RedoMenuItem(parent.getEditPane()));
		
		
		editMenu.addSeparator();
		menuItem = new JMenuItem(new DefaultEditorKit.CutAction());
		menuItem.setText("Cut");
		editMenu.add(menuItem);
		menuItem = new JMenuItem(new DefaultEditorKit.CopyAction());
		menuItem.setText("Copy");
		editMenu.add(menuItem);
		menuItem = new JMenuItem(new DefaultEditorKit.PasteAction());
		menuItem.setText("Paste");
		editMenu.add(menuItem);
		editMenu.add(new EventDeleteEdit("Delete",parent));
		editMenu.addSeparator();
		editMenu.add(new EventSelectAllEdit("Select All", parent));
		editMenu.add(new EventTimeEdit("Time", parent));
		editMenu.add(new EventDateEdit("Date", parent));

		
		JMenu toolsMenu = new JMenu("Tools");
		toolsMenu.addMenuListener(new ToolsMenuListener(parent, 2));
		add(toolsMenu);
		toolsMenu.add(new EventOptions(new String("Options..."), parent));
		toolsMenu.addSeparator();
		toolsMenu.add(new EventSynchronize(new String("Synchronize..."), parent));
		
		
					
	}
	
	
	
}

